#include <string>
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include "error.hpp"
class util
{
public:
    // 从指定文件中读取资源
    static bool ReadOnefile(const std::string &path, std::string *fileContent)
    {
        struct stat st;
        int ret = stat(path.c_str(), &st);
        if(ret < 0)//这里非常关键，因为后面会发起个./wwwroot/favicon.ico,如果文件不存在,后面打开就会出错
            return false;
        (*fileContent).resize(st.st_size);
        int fd = open(path.c_str(), O_RDONLY);
        if(fd < 0)
            return false;
        int n = read(fd, (char *)fileContent->c_str(), st.st_size);
        close(fd);
        if (n < 0)
        {
            std::cout << "Read err" << std::endl;
            return false;
        }
        return true;
    }
    static std::string ReadOneline(std::string &Request,const std::string sep)
    {
        int pos = 0;
        std::string ret;
        int nextpos = Request.find(sep.c_str(), pos);//截取一行数据返回
        if(nextpos == -1)
            return ret;
        ret  = Request.substr(pos, nextpos - pos + sep.size());
        Request.erase(0,nextpos + sep.size());//删除被读完的数据
        return ret; 
    }
};